package com.cchr.acms.tags;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import org.apache.commons.lang.StringUtils;

public class YesNoSelectTag extends SimpleTagSupport {

	String id;
	String name;
	String clazz;
	String style;
	String selected;
	boolean disabled;
	boolean select2Placeholder;
	
	private String[] labels = {"yes", "no"};
	private String[] values = {"1", "0"};
	
	@Override
	public void doTag() throws JspException, IOException {
		StringBuilder sb = new StringBuilder();
		sb.append("<select ");
		if(disabled)
		{
			sb.append("disabled=\"disabled\" ");
		}
		if(StringUtils.isNotBlank(id))
		{
			sb.append("id=\"").append(id).append("\" ");
		}
		if(StringUtils.isNotBlank(name))
		{
			sb.append("name=\"").append(name).append("\" ");
		}
		if(StringUtils.isNotBlank(clazz))
		{
			sb.append("class=\"").append(clazz).append("\" ");
		}
		if(StringUtils.isNotBlank(style))
		{
			sb.append("style=\"").append(style).append("\" ");
		}
		sb.append(">");
		if(select2Placeholder)
		{
			sb.append("<option></option>");
		}
		for(int i=0; i<values.length; i++)
		{
			sb.append("<option value=\"").append(values[i]).append("\"");
			if(values[i].equals(selected))
			{
				sb.append(" selected=\"selected\"");
			}
			sb.append(">").append(labels[i]).append("</option>");
		}
		sb.append("</select>");
		
		PageContext pc = (PageContext)getJspContext(); 
		JspWriter out = pc.getOut(); 
		out.println(sb.toString());
	}

	public String getSelected() {
		return selected;
	}

	public void setSelected(String selected) {
		this.selected = selected;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getClazz() {
		return clazz;
	}

	public void setClazz(String clazz) {
		this.clazz = clazz;
	}

	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}

	public boolean isDisabled() {
		return disabled;
	}

	public void setDisabled(boolean disabled) {
		this.disabled = disabled;
	}

	public boolean isSelect2Placeholder() {
		return select2Placeholder;
	}

	public void setSelect2Placeholder(boolean select2Placeholder) {
		this.select2Placeholder = select2Placeholder;
	}

}
